/**
 * 
 */
package com.infodms.dms.actions.sales.ordermanage.orderreport;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;

import com.infodms.dms.bean.AclUserBean;
import com.infodms.dms.common.Constant;
import com.infodms.dms.common.ErrorCodeConstant;
import com.infodms.dms.common.getCompanyId.GetOemcompanyId;
import com.infodms.dms.dao.sales.ordermanage.orderreport.ErpDataImportDao;
import com.infodms.dms.dao.sales.ordermanage.orderreport.OrderReportDao;
import com.infodms.dms.exception.BizException;
import com.infodms.dms.po.TipVsDlvryDtlPO;
import com.infodms.dms.po.TipVsDlvryPO;
import com.infodms.dms.po.TipVsOrderDetailPO;
import com.infodms.dms.po.TipVsOrderPO;
import com.infodms.dms.po.TipVsOrderReservePO;
import com.infodms.dms.po.TmDateSetPO;
import com.infodms.dms.po.TmDealerPO;
import com.infodms.dms.po.TmVehiclePO;
import com.infodms.dms.po.TmVhclMaterialPO;
import com.infodms.dms.po.TmVsAddressPO;
import com.infodms.dms.po.TmWarehousePO;
import com.infodms.dms.po.TmpFundMappingPO;
import com.infodms.dms.po.TtVsDlvryDtlInvoicePO;
import com.infodms.dms.po.TtVsDlvryDtlPO;
import com.infodms.dms.po.TtVsDlvryErpDtlPO;
import com.infodms.dms.po.TtVsDlvryErpPO;
import com.infodms.dms.po.TtVsDlvryMchPO;
import com.infodms.dms.po.TtVsDlvryPO;
import com.infodms.dms.po.TtVsDlvryReqDtlPO;
import com.infodms.dms.po.TtVsDlvryReqPO;
import com.infodms.dms.po.TtVsOrderDetailPO;
import com.infodms.dms.po.TtVsOrderPO;
import com.infodms.dms.po.TtVsOrderResourceReservePO;
import com.infodms.dms.util.DateTimeUtil;
import com.infodms.dms.util.sequenceUitl.SequenceManager;
import com.infoservice.mvc.context.ActionContext;
import com.infoservice.mvc.context.RequestWrapper;
import com.infoservice.po3.bean.PO;

/**
 * @author Administrator
 * 
 */
public class ErpDataImport {
	private Logger logger = Logger.getLogger(ErpDataImport.class);
	private ActionContext act = ActionContext.getContext();
	RequestWrapper request = act.getRequest();
	private final ErpDataImportDao dao = ErpDataImportDao.getInstance();
	private final OrderReportDao reportDao = OrderReportDao.getInstance();

	private final String EXECUTE_PRE_URL = "/jsp/sales/ordermanage/orderreport/executePre.jsp";// 执行页面

	public void executePre() {
		AclUserBean logonUser = (AclUserBean) act.getSession().get(Constant.LOGON_USER);
		try {
			act.setForword(EXECUTE_PRE_URL);
		} catch (Exception e) {
			BizException e1 = new BizException(act, e, ErrorCodeConstant.QUERY_FAILURE_CODE,
					"数据导入PRE");
			logger.error(logonUser, e1);
			act.setException(e1);
		}
	}

	public void execute() {
		AclUserBean logonUser = (AclUserBean) act.getSession().get(Constant.LOGON_USER);
		try {
			Long beginTime = System.currentTimeMillis();
			Long oemCompanyId = GetOemcompanyId.getOemCompanyId(logonUser);

			// 获得订单临时表
			TipVsOrderPO tipOrder = new TipVsOrderPO();
			tipOrder.setIsErr("0");
			// tipOrder.setPoOrderNo("DJC06077100929003");

			List<PO> tipOrderList = dao.select(tipOrder);
			for (int i = 0; i < tipOrderList.size(); i++) {
				tipOrder = (TipVsOrderPO) tipOrderList.get(i);
				TmDealerPO orderDealer = dao.getTmDealerByDealerCode(tipOrder.getOrderDealer()); // 订货方
				TmDealerPO billingDealer = dao.getTmDealerByDealerCode(tipOrder.getBillingDealer());// 开票方
				TmDealerPO receiveDealer = dao.getTmDealerByDealerCode(tipOrder.getReceiveDealer());// 收货方
				Long reqId = new Long(SequenceManager.getSequence(""));
				Long deliveryId = new Long(SequenceManager.getSequence(""));
				String orderNo = tipOrder.getPoOrderNo();

				Double orderPrice = new Double(0); // 订单总价
				Double discount = new Double(0);// 订单折扣
				Double reqTotalPrice = new Double(0); // 发运申请总价
				Double reqDiscountPrice = new Double(0); // 发运申请总价
				Integer orderTotalAmount = new Integer(0);// 订单总量
				Integer reserveTotalAmount = new Integer(0);// 资源保留总量
				Integer matchTotalAmount = new Integer(0);// 配车总计
				// 获得订单明细临时表
				TipVsOrderDetailPO tipOrderDetail = new TipVsOrderDetailPO();
				tipOrderDetail.setHeadId(tipOrder.getHeadId());
				tipOrderDetail.setIsErr("0");
				List<PO> tipOrderDetailList = dao.select(tipOrderDetail);
				for (int j = 0; j < tipOrderDetailList.size(); j++) {
					tipOrderDetail = (TipVsOrderDetailPO) tipOrderDetailList.get(j);
					Double total = tipOrderDetail.getDiscountSPrice() * tipOrderDetail.getAmount();
					orderPrice += total;
					discount += 0;

					Long reqDetailId = new Long(SequenceManager.getSequence(""));
					Integer reserveAmount = new Integer(0);
					// 获得资源保留临时表
					TipVsOrderReservePO tipOrderReserve = new TipVsOrderReservePO();
					tipOrderReserve.setHeadId(new Long(tipOrder.getErpOrderNo()));
					tipOrderReserve.setLineId(tipOrderDetail.getLineId());
					tipOrderReserve.setIsErr("0");
					List<PO> tipOrderReserveList = dao.select(tipOrderReserve);
					for (int k = 0; k < tipOrderReserveList.size(); k++) {
						tipOrderReserve = (TipVsOrderReservePO) tipOrderReserveList.get(k);
						reserveAmount += tipOrderReserve.getAmount();

						TtVsOrderResourceReservePO orderResourceReservePO = new TtVsOrderResourceReservePO();
						orderResourceReservePO.setReserveId(new Long(SequenceManager
								.getSequence("")));
						orderResourceReservePO.setReqDetailId(reqDetailId);
						// 获得物料
						TmVhclMaterialPO materialPO = new TmVhclMaterialPO();
						materialPO.setMaterialCode(tipOrderReserve.getMaterialCode());
						List<PO> materialList = dao.select(materialPO);
						if (materialList.size() != 0) {
							materialPO = (TmVhclMaterialPO) materialList.get(0);
							orderResourceReservePO.setMaterialId(materialPO.getMaterialId());
						}
						orderResourceReservePO.setBatchNo(tipOrderReserve.getBatchNo());
						orderResourceReservePO.setAmount(tipOrderReserve.getAmount());
						Map<String, Object> tmpVwNumMap = dao.getTmpVwNumMap(tipOrder.getHeadId(),
								orderResourceReservePO.getMaterialId(), orderResourceReservePO
										.getBatchNo());
						Integer count = new Integer(0);
						if (tmpVwNumMap != null) {
							count = (Integer) tmpVwNumMap.get("V_NUM");
						}
						// 判断是否发运
						if (reserveAmount.intValue() != count.intValue()) {
							orderResourceReservePO
									.setReserveStatus(Constant.RESOURCE_RESERVE_STATUS_01);
						} else {
							orderResourceReservePO
									.setReserveStatus(Constant.RESOURCE_RESERVE_STATUS_03);
						}
						orderResourceReservePO.setOemCompanyId(oemCompanyId);
						// 获得仓库
						TmWarehousePO warehousePO = new TmWarehousePO();
						warehousePO.setWarehouseCode(tipOrderReserve.getWareHouse());
						warehousePO.setErpWarehouseCode(tipOrderReserve.getWareHouseCode());
						warehousePO.setWarehouseType(Constant.WAREHOUSE_TYPE_01);
						List<PO> warehouseList = dao.select(warehousePO);
						if (warehouseList.size() != 0) {
							warehousePO = (TmWarehousePO) warehouseList.get(0);
							orderResourceReservePO.setWarehouseId(warehousePO.getWarehouseId());
						}
						orderResourceReservePO.setDeliveryAmount(count);
						orderResourceReservePO.setReserveType(Constant.RESERVE_TYPE_01);
						orderResourceReservePO.setCreateBy(new Long(119));
						orderResourceReservePO.setCreateDate(new Date());
						dao.insert(orderResourceReservePO);// 保存订单资源保留
					}
					// 已发运时保留数量为提报数量
					if (tipOrder.getStatus().equals("1")) {
						reserveAmount = tipOrderDetail.getAmount();
					}

					orderTotalAmount += tipOrderDetail.getAmount();
					reserveTotalAmount += reserveAmount;

					// 获得配车列表
					List<Map<String, Object>> matchLIst = dao.getTipVsDlvryDtlList(tipOrder
							.getHeadId(), tipOrderDetail.getMaterialCode());
					Integer matchAmount = new Integer(matchLIst.size());
					matchTotalAmount += matchAmount;

					TtVsOrderDetailPO orderDetailPO = new TtVsOrderDetailPO();
					orderDetailPO.setDetailId(tipOrderDetail.getLineId());
					orderDetailPO.setOrderId(tipOrder.getHeadId());
					// 获得物料列表
					TmVhclMaterialPO materialPO = new TmVhclMaterialPO();
					materialPO.setMaterialCode(tipOrderDetail.getMaterialCode());
					List<PO> materialList = dao.select(materialPO);
					if (materialList.size() != 0) {
						materialPO = (TmVhclMaterialPO) materialList.get(0);
						orderDetailPO.setMaterialId(materialPO.getMaterialId());
					}
					orderDetailPO.setOrderAmount(tipOrderDetail.getAmount());
					orderDetailPO.setCheckAmount(reserveAmount);
					orderDetailPO.setCallAmount(reserveAmount);
					orderDetailPO.setDeliveryAmount(reserveAmount);
					orderDetailPO.setMatchAmount(matchAmount);
					orderDetailPO.setSinglePrice(tipOrderDetail.getDiscountSPrice());
					orderDetailPO.setTotalPrice(total);
					orderDetailPO.setVer(new Integer(0));
					orderDetailPO.setCreateBy(new Long(119));
					orderDetailPO.setCreateDate(new Date());
					orderDetailPO.setDiscountRate(new Float(0));
					orderDetailPO.setDiscountSPrice(tipOrderDetail.getDiscountSPrice());
					orderDetailPO.setDiscountPrice(new Double(0));

					dao.insert(orderDetailPO);// 订单明细保存

					Double reqPrice = orderDetailPO.getDiscountSPrice() * reserveAmount;
					Double reqDiscount = orderDetailPO.getSinglePrice() * reserveAmount
							* orderDetailPO.getDiscountRate();
					reqTotalPrice += reqPrice;
					reqDiscountPrice += reqDiscount;

					TtVsDlvryReqDtlPO dlvryReqDtlPO = new TtVsDlvryReqDtlPO();
					dlvryReqDtlPO.setDetailId(reqDetailId);
					dlvryReqDtlPO.setReqId(reqId);
					dlvryReqDtlPO.setOrderDetailId(orderDetailPO.getDetailId());
					dlvryReqDtlPO.setMaterialId(orderDetailPO.getMaterialId());
					dlvryReqDtlPO.setReqAmount(reserveAmount);
					dlvryReqDtlPO.setDeliveryAmount(reserveAmount);
					dlvryReqDtlPO.setVer(new Integer(0));
					dlvryReqDtlPO.setCreateBy(orderDetailPO.getCreateBy());
					dlvryReqDtlPO.setCreateDate(orderDetailPO.getCreateDate());
					dlvryReqDtlPO.setSinglePrice(orderDetailPO.getSinglePrice());
					dlvryReqDtlPO.setTotalPrice(orderDetailPO.getDiscountSPrice() * reserveAmount);
					dlvryReqDtlPO.setDiscountRate(orderDetailPO.getDiscountRate());
					dlvryReqDtlPO.setDiscountSPrice(orderDetailPO.getDiscountSPrice());
					dlvryReqDtlPO.setDiscountPrice(orderDetailPO.getDiscountPrice());
					dlvryReqDtlPO.setReserveAmount(reserveAmount);

					dao.insert(dlvryReqDtlPO);// 发运申请明细保存

					TtVsDlvryDtlPO dlvryDtlPO = new TtVsDlvryDtlPO();
					dlvryDtlPO.setDetailId(new Long(SequenceManager.getSequence("")));
					dlvryDtlPO.setDeliveryId(deliveryId);
					dlvryDtlPO.setReqId(reqId);
					dlvryDtlPO.setMaterialId(dlvryReqDtlPO.getMaterialId());
					dlvryDtlPO.setReqDtlId(dlvryReqDtlPO.getDetailId());
					dlvryDtlPO.setDeliveryAmount(reserveAmount);
					dlvryDtlPO.setMatchAmount(matchAmount);
					dlvryDtlPO.setOutAmount(matchAmount);
					dlvryDtlPO.setVer(new Integer(0));
					dlvryDtlPO.setCreateBy(dlvryReqDtlPO.getCreateBy());
					dlvryDtlPO.setCreateDate(dlvryReqDtlPO.getCreateDate());
					dlvryDtlPO.setBillingCode(tipOrderDetail.getBillNo());
					dlvryDtlPO.setBillingAmount(total);
					dlvryDtlPO.setBillingDate(DateTimeUtil.stringToDateByPattern(tipOrder
							.getReportDate(), "yyyy-MM-dd"));
					dlvryDtlPO.setInspectionAmount(new Integer(0));

					dao.insert(dlvryDtlPO);// 发运单明细保存

					for (int m = 0; m < matchLIst.size(); m++) {
						Map<String, Object> mchMap = matchLIst.get(m);
						TtVsDlvryMchPO dlvryMchPO = new TtVsDlvryMchPO();
						dlvryMchPO.setMatchId(new Long(SequenceManager.getSequence("")));
						dlvryMchPO.setDeliveryDetailId(dlvryDtlPO.getDetailId());
						// 获得车辆PO
						String vin = (String) mchMap.get("VIN");
						TmVehiclePO vehiclePO = new TmVehiclePO();
						vehiclePO.setVin(vin);
						List<PO> vehicleList = dao.select(vehiclePO);
						if (vehicleList.size() != 0) {
							vehiclePO = (TmVehiclePO) vehicleList.get(0);
							dlvryMchPO.setVehicleId(vehiclePO.getVehicleId());

							TmVehiclePO tvp1 = new TmVehiclePO();
							tvp1.setVehicleId(vehiclePO.getVehicleId());
							TmVehiclePO tvp2 = new TmVehiclePO();
							if (billingDealer != null) {
								tvp2.setDealerId(billingDealer.getDealerId());
							}
							tvp2.setLifeCycle(Constant.VEHICLE_LIFE_05);
							dao.update(tvp1, tvp2);// 车辆状态更新
						}
						dlvryMchPO.setStatus(Constant.STATUS_ENABLE);
						dlvryMchPO.setCreateBy(dlvryDtlPO.getCreateBy());
						dlvryMchPO.setCreateDate(dlvryDtlPO.getCreateDate());
						BigDecimal sendId = (BigDecimal) mchMap.get("HEAD_ID");
						dlvryMchPO.setErpSendcarId(sendId.longValue());
						dlvryMchPO.setIfInspection(new Integer(0));

						dao.insert(dlvryMchPO);// 配车信息保存
					}

					TtVsDlvryDtlInvoicePO dlvryDtlInvoicePO = new TtVsDlvryDtlInvoicePO();
					dlvryDtlInvoicePO.setDetailId(new Long(SequenceManager.getSequence("")));
					dlvryDtlInvoicePO.setOrderId(deliveryId);
					dlvryDtlInvoicePO.setSoNo(tipOrder.getErpOrderNo());
					dlvryDtlInvoicePO.setInvoiceNo(tipOrderDetail.getBillNo());
					dlvryDtlInvoicePO.setMaterialCode(materialPO.getMaterialCode());
					dlvryDtlInvoicePO.setErpId(materialPO.getErpId());
					dlvryDtlInvoicePO.setQuantity(new Long(tipOrderDetail.getAmount()));
					dlvryDtlInvoicePO.setAmount(tipOrderDetail.getDiscountSPrice());
					dlvryDtlInvoicePO.setCompanyId(oemCompanyId);
					dlvryDtlInvoicePO.setStatus(new Integer(1));
					dlvryDtlInvoicePO.setOrderNo(orderNo);
					dlvryDtlInvoicePO.setCreateBy(new Long(119));
					dlvryDtlInvoicePO.setCreateDate(new Date());

					dao.insert(dlvryDtlInvoicePO); // 发票明细保存
				}

				TtVsOrderPO orderPO = new TtVsOrderPO();
				orderPO.setOrderId(tipOrder.getHeadId());
				orderPO.setCompanyId(oemCompanyId);
				orderPO.setAreaId(new Long(tipOrder.getArea()));
				orderPO.setOrderNo(orderNo);
				orderPO.setOrderType(Constant.ORDER_TYPE_02);
				orderPO.setIsRefitOrder(new Integer(0));
				if (tipOrder.getReportDate() != null) {
					TmDateSetPO dateSet = reportDao.getTmDateSetPO(DateTimeUtil
							.stringToDateByPattern(tipOrder.getReportDate(), "yyyy-MM-dd"),
							oemCompanyId);
					if (dateSet != null) {
						String orderYear = dateSet.getSetYear();
						String orderMonth = dateSet.getSetMonth();
						String orderWeek = dateSet.getSetWeek();
						orderPO.setOrderYear(new Integer(orderYear));
						orderPO.setOrderMonth(new Integer(orderMonth));
						orderPO.setOrderWeek(new Integer(orderWeek));
					}
				}
				orderPO.setOrderOrgType(Constant.ORG_TYPE_DEALER);

				if (orderDealer != null) {
					orderPO.setOrderOrgId(orderDealer.getDealerId());
				}
				orderPO.setBillingOrgType(new Long(Constant.ORG_TYPE_DEALER.longValue()));
				if (billingDealer != null) {
					orderPO.setBillingOrgId(billingDealer.getDealerId());
				}
				if (receiveDealer != null) {
					orderPO.setReceiver(receiveDealer.getDealerId());
				}
				if (tipOrder.getReportDate() != null) {
					orderPO.setRaiseDate(DateTimeUtil.stringToDateByPattern(tipOrder
							.getReportDate(), "yyyy-MM-dd"));
				}

				// 获得资金类型
				TmpFundMappingPO fundMappping = new TmpFundMappingPO();
				fundMappping.setErpFundType(tipOrder.getFundType());
				List<PO> fundList = dao.select(fundMappping);
				if (fundList.size() != 0) {
					fundMappping = (TmpFundMappingPO) fundList.get(0);
					orderPO.setFundTypeId(new Long(fundMappping.getDmsFundTypeId()));
				}
				orderPO.setOrderPrice(orderPrice);
				if (tipOrder.getDeliveryType().equals("自提")) {
					orderPO.setDeliveryType(Constant.TRANSPORT_TYPE_01);
				} else {
					orderPO.setDeliveryType(Constant.TRANSPORT_TYPE_02);
				}
				// 查询地址是否存在，不存在则新建
				TmVsAddressPO addressPO = new TmVsAddressPO();
				addressPO.setDealerId(orderPO.getReceiver());
				addressPO.setAddress(tipOrder.getDeliveryAddress());
				List<PO> addList = dao.select(addressPO);
				if (addList.size() != 0) {
					addressPO = (TmVsAddressPO) addList.get(0);
					orderPO.setDeliveryAddress(addressPO.getId());
				} else {
					addressPO = new TmVsAddressPO();
					Long id = new Long(SequenceManager.getSequence(""));
					addressPO.setId(id);
					addressPO.setAddress(tipOrder.getDeliveryAddress());
					addressPO.setLinkMan(tipOrder.getLinkMan());
					addressPO.setTel(tipOrder.getTel());
					addressPO.setStatus(Constant.STATUS_ENABLE);
					addressPO.setDealerId(orderPO.getReceiver());
					dao.insert(addressPO);
					orderPO.setDeliveryAddress(id);
				}
				orderPO.setOrderRemark(tipOrder.getRemark());
				orderPO.setOrderStatus(Constant.ORDER_STATUS_05);
				orderPO.setVer(new Integer(0));
				orderPO.setCreateBy(new Long(119));
				orderPO.setCreateDate(new Date());
				orderPO.setPriceId(new Long(tipOrder.getPriceType()));
				orderPO.setLinkMan(tipOrder.getLinkMan());
				orderPO.setTel(tipOrder.getTel());
				orderPO.setDiscount(discount);
				dao.insert(orderPO);// 订单保存

				TtVsDlvryReqPO dlvryReqPO = new TtVsDlvryReqPO();
				dlvryReqPO.setReqId(reqId);
				dlvryReqPO.setAreaId(orderPO.getAreaId());
				dlvryReqPO.setOrderId(orderPO.getOrderId());
				dlvryReqPO.setFundType(orderPO.getFundTypeId());
				dlvryReqPO.setDeliveryType(orderPO.getDeliveryType());
				dlvryReqPO.setAddressId(orderPO.getDeliveryAddress());
				dlvryReqPO.setReqDate(orderPO.getRaiseDate());
				dlvryReqPO.setReqTotalAmount(reserveTotalAmount);
				dlvryReqPO.setReqExecStatus(Constant.REQ_EXEC_STATUS_01);
				dlvryReqPO.setVer(new Integer(0));
				dlvryReqPO.setCreateBy(orderPO.getCreateBy());
				dlvryReqPO.setCreateDate(orderPO.getCreateDate());
				dlvryReqPO.setPriceId(orderPO.getPriceId());
				dlvryReqPO.setReceiver(orderPO.getReceiver());
				dlvryReqPO.setLinkMan(orderPO.getLinkMan());
				dlvryReqPO.setTel(orderPO.getTel());
				dlvryReqPO.setDiscount(reqDiscountPrice);
				dlvryReqPO.setModifyFlag(new Integer(0));
				// 获得仓库
				TmWarehousePO warehousePO = new TmWarehousePO();
				warehousePO.setErpOrganizationId(new Long(tipOrder.getWarehouseOrg()));
				warehousePO.setErpWarehouseCode(tipOrder.getWarehouseCode());
				warehousePO.setWarehouseType(Constant.WAREHOUSE_TYPE_01);
				List<PO> warehouseList = dao.select(warehousePO);
				if (warehouseList.size() != 0) {
					warehousePO = (TmWarehousePO) warehouseList.get(0);
					dlvryReqPO.setWarehouseId(warehousePO.getWarehouseId());
				}
				if (tipOrder.getStatus().equals("0")) {
					dlvryReqPO.setReqStatus(Constant.ORDER_REQ_STATUS_04);
				} else {
					dlvryReqPO.setReqStatus(Constant.ORDER_REQ_STATUS_06);
				}
				dlvryReqPO.setReqTotalPrice(reqTotalPrice);
				dlvryReqPO.setReserveTotalAmount(reserveTotalAmount);
				dlvryReqPO.setDlvryReqNo(orderPO.getOrderNo());
				dao.insert(dlvryReqPO);// 发运申请保存

				TtVsDlvryPO dlvryPO = new TtVsDlvryPO();
				dlvryPO.setDeliveryId(deliveryId);
				dlvryPO.setReqId(reqId);
				dlvryPO.setDeliveryNo(dlvryReqPO.getDlvryReqNo());
				dlvryPO.setOrderId(dlvryReqPO.getOrderId());
				dlvryPO.setFundType(dlvryReqPO.getFundType());
				dlvryPO.setDeliveryType(dlvryReqPO.getDeliveryType());
				dlvryPO.setAddressId(dlvryReqPO.getAddressId());
				dlvryPO.setDeliveryDate(dlvryReqPO.getReqDate());
				if (tipOrder.getStatus().equals("0")) {
					if (matchTotalAmount.intValue() > 0) {
						dlvryPO.setDeliveryStatus(Constant.DELIVERY_STATUS_10);
					} else {
						dlvryPO.setDeliveryStatus(Constant.DELIVERY_STATUS_04);
					}
				} else {
					dlvryPO.setDeliveryStatus(Constant.DELIVERY_STATUS_05);
				}
				dlvryPO.setVer(new Integer(0));
				dlvryPO.setCreateBy(dlvryReqPO.getCreateBy());
				dlvryPO.setCreateDate(dlvryReqPO.getCreateDate());
				dlvryPO.setPriceId(dlvryReqPO.getPriceId());
				dlvryPO.setReceiver(dlvryReqPO.getReceiver());
				dlvryPO.setLinkMan(dlvryReqPO.getLinkMan());
				dlvryPO.setTel(dlvryReqPO.getTel());
				dlvryPO.setDiscount(dlvryReqPO.getDiscount());
				dlvryPO.setBillingSide(orderPO.getBillingOrgId());
				dlvryPO.setErpOrder(tipOrder.getErpOrderNo());
				dlvryPO.setPayMent(dlvryReqPO.getFundType());
				dlvryPO.setCompanyId(oemCompanyId);
				dlvryPO.setWarehouseId(dlvryReqPO.getWarehouseId());

				dao.insert(dlvryPO);// 发运单保存

				// 获得送车交接单临时表
				TipVsDlvryPO tipDlvry = new TipVsDlvryPO();
				tipDlvry.setErpOrderId(tipOrder.getHeadId().toString());
				tipDlvry.setIsErr("0");
				List<PO> tipDlvryList = dao.select(tipDlvry);
				for (int n = 0; n < tipDlvryList.size(); n++) {
					tipDlvry = (TipVsDlvryPO) tipDlvryList.get(n);

					// 获得送车交接单明细临时表
					TipVsDlvryDtlPO tipDlvryDtl = new TipVsDlvryDtlPO();
					tipDlvryDtl.setHeadId(tipDlvry.getSendId());
					tipDlvryDtl.setIsErr("0");
					List<PO> tipDlvryDtlList = dao.select(tipDlvryDtl);
					for (int x = 0; x < tipDlvryDtlList.size(); x++) {
						tipDlvryDtl = (TipVsDlvryDtlPO) tipDlvryDtlList.get(x);

						TtVsDlvryErpDtlPO dlvryErpDtlPO = new TtVsDlvryErpDtlPO();
						dlvryErpDtlPO.setDetailId(tipDlvryDtl.getLineId());
						dlvryErpDtlPO.setSendcarHeaderId(tipDlvryDtl.getHeadId());
						dlvryErpDtlPO.setCarType(tipDlvryDtl.getSeries());
						dlvryErpDtlPO.setCarStatus(tipDlvryDtl.getStatus());
						dlvryErpDtlPO.setColor(tipDlvryDtl.getColor());
						dlvryErpDtlPO.setVin(tipDlvryDtl.getVin());
						dlvryErpDtlPO.setEnginerId(tipDlvryDtl.getEngineNo());

						TipVsOrderDetailPO tipOrderDetail2 = new TipVsOrderDetailPO();
						tipOrderDetail2.setHeadId(tipOrder.getHeadId());
						tipOrderDetail2.setMaterialCode(tipDlvryDtl.getMaterialCode());
						List<PO> detailList = dao.select(tipOrderDetail2);
						if (detailList.size() != 0) {
							tipOrderDetail2 = (TipVsOrderDetailPO) detailList.get(0);
							dlvryErpDtlPO.setInvoiceNumber(tipOrderDetail2.getBillNo());
						}
						dlvryErpDtlPO.setCreateBy(new Long(119));
						dlvryErpDtlPO.setCreateDate(new Date());

						dao.insert(dlvryErpDtlPO);// 送车交接单明细保存
					}

					TtVsDlvryErpPO dlvryErpPO = new TtVsDlvryErpPO();
					dlvryErpPO.setSendcarHeaderId(new BigDecimal(tipDlvry.getSendId()));
					dlvryErpPO.setOrderNumber(tipOrder.getErpOrderNo());
					dlvryErpPO.setSendcarOrderNumber(tipDlvry.getSendCode());
					dlvryErpPO.setShipMethodCode(tipDlvry.getSendOrg());
					dlvryErpPO.setMotorman(tipDlvry.getSender());
					dlvryErpPO.setMotormanPhone(tipDlvry.getSenderTel());
					dlvryErpPO.setFlatcarAssignDate(DateTimeUtil.stringToDateByPattern(tipDlvry
							.getSendDate(), "yyyy-MM-dd"));
					dlvryErpPO.setFlatcarId(tipDlvry.getVehicleNo());
					dlvryErpPO.setDeliveryId(dlvryPO.getDeliveryId().toString());
					dlvryErpPO.setSendcarAmount(new Integer(tipDlvryDtlList.size()));
					dlvryErpPO.setCreateBy(new Long(119));
					dlvryErpPO.setCreateDate(new Date());

					dao.insert(dlvryErpPO); // 送车交接单保存
				}
			}

			Long endTime = System.currentTimeMillis();
			Long minuts = (endTime - beginTime) / 1000;
			System.out.println("-----------minuts===" + minuts);

			act.setOutData("returnValue", 1);

		} catch (Exception e) {
			BizException e1 = new BizException(act, e, ErrorCodeConstant.ADD_FAILURE_CODE, "数据导入执行");
			logger.error(logonUser, e1);
			act.setException(e1);
		}
	}
}
